﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NHibernate;
using ZLCMS.Data.Model;
using ZLCMS.Business.Util;
using ZLCMS.Business.DwzFrame;

namespace ZLCMS.Web.SystemManage.Template.Web
{
    public partial class WXConfig : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string code = UserKit.GetUserAccountWithoutException();
            if (string.IsNullOrEmpty(code))
            {
                new FrameHelper(this).Alert(new ReturnCode()
                {
                    statusCode = 300,
                    message = "参数错误"
                });
                return;
            }
            if (!IsPostBack)
            {
                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    WxConfig config = session.CreateQuery("from WxConfig where OwnerAccount=:acc")
                        .SetParameter("acc", code)
                        .UniqueResult<WxConfig>();

                    if (config ==null)
                    {
                        config = new WxConfig();
                        config.InterfaceUrl = ServerUtil.GetRootURI() + "/wxInterface/" + code + "/index.xhtml";
                        config.InterfaceToken = GenerateToken();
                    }
                    wxaccount.Text = config.WxAccount;
                    token.Text = config.InterfaceToken;
                    addr.Text = config.InterfaceUrl;
                    appid.Text = config.AppId;
                    appsecret.Text = config.AppSecret;
                }

                //repeater1.DataBind();
            }
            else
            {

                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    WxConfig config = session.CreateQuery("from WxConfig where OwnerAccount=:acc")
                        .SetParameter("acc", code)
                        .UniqueResult<WxConfig>();


                    if (config == null)
                    {
                        config = new WxConfig();
                        config.InterfaceUrl = ServerUtil.GetRootURI() + "/wxInterface/" + ViewState["account"] + "/index.xhtml";

                    }
                    config.AppId = appid.Text;
                    config.AppSecret = appsecret.Text;
                    config.WxAccount = wxaccount.Text;
                    config.InterfaceToken = token.Text;
                    config.InterfaceUrl = addr.Text;
                    config.OwnerAccount = code;
                    session.SaveOrUpdate(config);
                    session.Flush();
                    new FrameHelper(this).Alert(new ReturnCode()
                    {
                        statusCode = 200,
                        message = "操作成功"
                    });
                }

            }
        }

        private string GenerateToken()
        {
            int number;
            char code;
            string checkCode = String.Empty;
            Random random = new Random();

            for (int i = 0; i < 3; i++)
            {
                number = random.Next();
                code = (char)('a' + (char)(number % 26));
                checkCode += code.ToString();
            }

            for (int i = 0; i < 5; i++)
            {
                number = random.Next();
                code = (char)('0' + (char)(number % 10));
                checkCode += code.ToString();
            }
            return checkCode;
        }

    }
}